package oop.factor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import oop.data.RowData;


/**
 * 读取文件中的数据，按行存储
 */
public class Reader {
    //存储符合条件的文件
    private List<File> fileList  ;
    private int rowNum;
    private List<RowData> datas;

    //将路径创建文件，加入文件数组
    public Reader(String path[],Integer rowNum) {
        this.rowNum = rowNum;
        fileList = new ArrayList<>();
        for(String str : path){
            File file = new File(str);
            fileList.add(file);
        }

    }
    
    public Integer size() {
        return datas.size();
    }
    
    public Iterator<RowData> getIterator() {
        //第一次获取仓库数据时初始化仓库数据
        if (datas == null) {
            initDatas();
        }
        return datas.iterator();
    }
    
//    public String getDescribe() {
//        return File.getName();
//    }
    public List<RowData> getDatas() {
        if (datas == null) {
            initDatas();
        }
        return datas;
    }
    private void initDatas() {
        datas = new ArrayList<RowData>();
        //遍历文件
        for(File file : fileList){

            try {
                FileInputStream in = new FileInputStream(file);
                InputStreamReader inReader = new InputStreamReader(in, "UTF-8");
                BufferedReader bufReader = new BufferedReader(inReader);


                String line = null;

                String[] columnArray = null;
                String[] dataArray = null;
                Map<String, String> mapData = null;

                line = bufReader.readLine();
                if (line != null) {
                    //columnArray 行标
                    columnArray = line.split(",");
                }

                System.out.println("Reading the " + file.getName() + ":");
                while ((line = bufReader.readLine()) != null) {
                    //忽略空行
                    String pattern = "\\s+";
                    boolean isBlank = Pattern.matches(pattern, line);
                    if (line.equals("") || isBlank) {
                        System.out.println("\t there has none-line!");
                        continue;
                    }


                    mapData = new HashMap<String, String>();

                    dataArray = line.split(",");

                    try {
                        for (int i = 0; i < columnArray.length; i++) {
                            //最后一列是空的情况单独处理
                            if (i == columnArray.length - 1 && dataArray.length == columnArray.length - 1) {
                                mapData.put(columnArray[i], "");
                                continue;
                            }
                            mapData.put(columnArray[i], dataArray[i]);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    String fatherName = "";
                    Pattern pattern1 = Pattern.compile("(.*)\\.csv");
                    Matcher matcher1 = pattern1.matcher(file.getName());
                    while(matcher1.find()){
                        fatherName = matcher1.group(1);
                    }

                    // 存储
                    datas.add(new RowData(fatherName,columnArray, mapData));
                }
                bufReader.close();
                inReader.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("读取出错！");
            }
        }

    }
    
    

}
